<?
/*
+-------------------------------------------------------------------------+
| Copyright (C) 2009: Xabi Ezpeleta <xezpeleta _at_ gmail.com>            |
|                                                                         |
| This program is free software; you can redistribute it and/or           |
| modify it under the terms of the GNU General Public License             |
| as published by the Free Software Foundation; either version 2          |
| of the License, or (at your option) any later version.                  |
|                                                                         |
| This program is distributed in the hope that it will be useful,         |
| but WITHOUT ANY WARRANTY; without even the implied warranty of          |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the           |
| GNU General Public License for more details.                            |
+-------------------------------------------------------------------------+
*/

include_once('config.php');


function check_url($url) {
    $c = curl_init();
    curl_setopt($c, CURLOPT_URL, $url);
    curl_setopt($c, CURLOPT_HEADER, 1); // get the header
    curl_setopt($c, CURLOPT_NOBODY, 1); // and *only* get the header
    curl_setopt($c, CURLOPT_RETURNTRANSFER, 1); // get the response as a string from curl_exec(), rather than echoing it
    curl_setopt($c, CURLOPT_FRESH_CONNECT, 1); // don't use a cached version of the url
    if (!curl_exec($c)) { return false; }

    $httpcode = curl_getinfo($c, CURLINFO_HTTP_CODE);
    #print "debug httpcode=".$httpcode;
	#return ($httpcode < 400);
	return ($httpcode == 200);
}


function generate_plx ($title, $logo, $background, $thumb, $url1stpart, $parts) {
	$now = time();

	echo "version=1\n";
	echo "title=$title\n";
	echo "logo=$logo\n";
	echo "background=$background\n";
	echo "#\n";
	
	for ($i=0; $i<10; $i++){
  		$date=mktime(0, 0, 0, date("m", $now)  , date("d", $now)-$i, date("Y", $now));
  		$day	= date("d", $date);
  		$month= date("m", $date);
  		$year	= date("y", $date);
  		$longyear= date("Y", $date);
		
  		for ($part=1; $part<$parts+1; $part++){
    		$videoURL = $url1stpart.$day."_".$month."_".$year."_".substr($part+100,1).".mp4";
   			 
    		if (check_url($videoURL)){
      			echo "type=video\n";
      			echo "name=".$title."_".date("md",$date)."_".substr($part+100,1)."\n";
      			echo "URL=".$videoURL."\n";
	  			echo "thumb=$thumb\n";
	  			echo "date=".date("Y-m-d",$date);
      			echo "\n#\n";
    		}else{
      			#echo "#Invalid URL: $videoURL";
      			#so, exit and check with other date...
      			$part=$parts;
    		}
  		}
	}
}

?>
